from Storage import StylesStorage

__author__ = 'deadmoroz'

class OutputBuilder(object):
    def open_mode(self, mode_name):
        raise NotImplemented()

    def close_mode(self):
        raise NotImplemented()

    def append_keyword(self, keyword_class, keyword_body):
        raise NotImplemented()

    def append_text(self, text):
        raise NotImplemented()

    def get_content(self):
        raise NotImplemented()


class HtmlOutputBuilder(OutputBuilder):
    __header_template = """
        <html>
        <head>
            <style>
                %(content)s
            </style>
        </head>
        <body>
        <pre><code>
"""

    def __init__(self, style_name):
        self.__styleRepository = StylesStorage()
        style = self.__styleRepository[style_name]

        self.__header = self.__header_template % {"content": style}

        self.__footer = """
        </pre>
        </code>
        </body>
        </html>
"""
        self.__body = []

    def open_mode(self, mode_name):
        self.__body.append("<span class=\"" + mode_name + "\">")

    def close_mode(self):
        self.__body.append("</span>")

    def append_keyword(self, keyword_class, keyword_body):
        self.__body.append(
            "<span class=\"%(class)s\">%(content)s</span>" % {"class": keyword_class, "content": keyword_body})

    def append_text(self, text):
        self.__body.append(text)

    def get_content(self):
        return self.__header + "".join(self.__body) + self.__footer


class TestOutputBuilder(OutputBuilder):
    def __init__(self):
        self.__body = []

    def open_mode(self, mode_name):
        self.__body.append("<span class=\"" + mode_name + "\">")

    def close_mode(self):
        self.__body.append("</span>")

    def append_keyword(self, keyword_class, keyword_body):
        self.__body.append(
            "<span class=\"%(class)s\">%(content)s</span>" % {"class": keyword_class, "content": keyword_body})

    def append_text(self, text):
        self.__body.append(text)

    def get_content(self):
        return "".join(self.__body)
